package com.bootdo.fx.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.*;
import com.bootdo.fx.domain.UserInfoDO;
import com.bootdo.fx.service.UserInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.fx.domain.SiteMsgDO;
import com.bootdo.fx.service.SiteMsgService;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * 
 * @author 袁飞龙
 * @email 1992lcg@163.com
 * @date 2017-11-30 17:12:20
 */
 
@Controller
public class SiteMsgController {
	@Autowired
	private SiteMsgService siteMsgService;

	@Autowired
	private UserInfoService userInfoService;
	
	@GetMapping("/fx/siteMsg")
	@RequiresPermissions("fx:siteMsg:siteMsg")
	String SiteMsg(){
	    return "fx/siteMsg/siteMsg";
	}
	
	@ResponseBody
	@GetMapping("/fx/siteMsg/list")
	@RequiresPermissions("fx:siteMsg:siteMsg")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<SiteMsgDO> siteMsgList = siteMsgService.list(query);
		int total = siteMsgService.count(query);
		PageUtils pageUtils = new PageUtils(siteMsgList, total);
		return pageUtils;
	}
	
	@GetMapping("/fx/siteMsg/add")
	@RequiresPermissions("fx:siteMsg:add")
	String add(){
	    return "fx/siteMsg/add";
	}

	@GetMapping("/fx/siteMsg/edit/{msgId}")
	@RequiresPermissions("fx:siteMsg:edit")
	String edit(@PathVariable("msgId") Long msgId,Model model){
		SiteMsgDO siteMsg = siteMsgService.get(msgId);
		model.addAttribute("siteMsg", siteMsg);
	    return "fx/siteMsg/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/fx/siteMsg/save")
	@RequiresPermissions("fx:siteMsg:add")
	public R save( SiteMsgDO siteMsg){
		if(siteMsgService.save(siteMsg)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/fx/siteMsg/update")
	@RequiresPermissions("fx:siteMsg:edit")
	public R update( SiteMsgDO siteMsg){
		siteMsgService.update(siteMsg);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/siteMsg/remove")
	@ResponseBody
	@RequiresPermissions("fx:siteMsg:remove")
	public R remove( Long msgId){
		if(siteMsgService.remove(msgId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/siteMsg/batchRemove")
	@ResponseBody
	@RequiresPermissions("fx:siteMsg:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] msgIds){
		siteMsgService.batchRemove(msgIds);
		return R.ok();
	}



	/*
    * 展示系统消息
    * */
	@RequestMapping("/wx/siteMsg/listSiteMsg")
	@ResponseBody
	public JsonResult listSiteMsg(HttpServletRequest request){
		Map<String,Object> map = new HashMap<>();
		//UserInfoDO userInfo = (UserInfoDO) request.getSession().getAttribute("userInfo");
		UserInfoDO userInfo = LoginUtils.getCurrentUser(request);
		map.put("userId",userInfo.getUserId());
		return ResultUtil.renderSuccess(siteMsgService.list(map));
	}
}
